﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace MailServer.Persistence
{
    /// <summary>
    /// Para Guardar la Clase en el Disco
    /// </summary>
    public class Persistence
    {
        /// <summary>
        /// Serializa un objeto y lo almacena en el disco
        /// </summary>
        /// <param name="theObject"></param>
        /// <param name="path"></param>
        public static void Serialize(object theObject, string path)
        {
   
            Stream stream = File.Open(path, FileMode.Create);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, theObject);
            stream.Close();
        }
        /// <summary>
        /// Deserealiza un objeto de un Archivo
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static object Deserealize(string path)
        {
            try
            {
                object theObject = null;
                Stream stream = File.Open(path, FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();
                theObject = formatter.Deserialize(stream);
                stream.Close();
                return theObject;
            }
            catch
            {
                return null;
            }
        }
    }
}
